package com.webtest.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.Collection;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/8/13 11:54
 */
public class ForEachTag extends SimpleTagSupport {
    private Collection<?> items;
    private String var;

    public void setItems(Collection<?> items) {
        this.items = items;
    }

    public void setVar(String var) {
        this.var = var;
    }

    @Override
    public void doTag() throws JspException, IOException {
        //1.遍历items集合
        if (items != null) {
            for (Object item : items) {
                // 把正在遍历的对象放入到pageContext中，键：var，值：正在遍历对象
                getJspContext().setAttribute(var, item);
                // 把标签的内容输出到页面上
                getJspBody().invoke(null);
            }
        }
    }
}
